﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Vote
{
    public partial class VoteStatistics : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int id;
                #region Get id

                if (string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    Common.JShelper.JSAlert(Page, "err", "参数错误!");
                    return;
                }

                try
                {
                    id = int.Parse(Request.QueryString["id"]);
                }
                catch
                {
                    Common.JShelper.JSAlert(Page, "err", "参数错误!");
                    return;
                }
                #endregion

                LabMS.Model.Vote mVote = new LabMS.Model.Vote();
                #region Get  Vote

                LabMS.BLL.Vote bvote = new LabMS.BLL.Vote();
                mVote = bvote.GetModel(id);
                if (mVote == null)
                {
                    Common.JShelper.JSAlert(Page, "err", "参数错误!");
                    return;
                }
                #endregion

                #region Original and obsolete

                /*

                lbVName.Text = mVote.VoteTitle;
                lbContent.InnerHtml = "<div>";

                List<LabMS.Model.VoteItem> listVItem = new List<LabMS.Model.VoteItem>();
                List<LabMS.Model.VoteAnswer> listVAnswer = new List<LabMS.Model.VoteAnswer>();
                List<LabMS.Model.VoteRecordDetail> listVRDetail
                    = new List<LabMS.Model.VoteRecordDetail>();

                #region VoteItemList
                LabMS.BLL.VoteItem bVItem = new LabMS.BLL.VoteItem();
                listVItem = bVItem.GetModelList(" VoteID = " + mVote.key.ToString());
                if (listVItem == null)
                {
                    Common.JShelper.JSAlert(Page, "err", "参数错误!");
                    return;
                }
                #endregion

                int iTotal = 0;
                int iTotalD = 0;
                int iDetail = 0;
                float percent = 0.0f;

                foreach (LabMS.Model.VoteItem vi in listVItem)
                {
                    lbContent.InnerHtml += "<div>";
                    lbContent.InnerHtml += "     " + vi.VoteItemContent;

                    #region Get VoteItemDetail for total
                    LabMS.BLL.VoteRecordDetail bVRDetail = new LabMS.BLL.VoteRecordDetail();
                    listVRDetail = bVRDetail.GetModelList(" VoteItemID = " + vi.key.ToString());
                    #endregion

                    iTotal = listVRDetail.Count;
                    iTotalD = iTotal;
                    if (iTotalD == 0)
                    {
                        iTotalD = 1;
                    }
                    lbTotal.Text = "投票总数:" + iTotal.ToString();

                    #region VoteAnswerList
                    LabMS.BLL.VoteAnswer bVAnswer = new LabMS.BLL.VoteAnswer();
                    listVAnswer = bVAnswer.GetModelList(" VoteItemID = " + vi.key.ToString());

                    #endregion
                    foreach (LabMS.Model.VoteAnswer va in listVAnswer)
                    {
                        lbContent.InnerHtml += "<div>";
                        lbContent.InnerHtml += "          " + va.VoteAnswerContent;

                        #region Get VoteItemDetail for answer
                        bVRDetail = new LabMS.BLL.VoteRecordDetail();
                        listVRDetail = bVRDetail.GetModelList(" VoteItemID = " + vi.key.ToString()
                                                                                    + " and VoteAnswerID = " + va.key.ToString());
                        #endregion
                        iDetail = listVRDetail.Count;
                        percent = (float)iDetail / (float)iTotalD;
                        percent = percent * 100;
                        string strPercent = String.Format("{0:N2}", percent) + "%";
                        lbContent.InnerHtml += "          " + iDetail + "(" + strPercent.ToString() + ")";

                        lbContent.InnerHtml += "</div>";
                    }

                    lbContent.InnerHtml += "</div>";
                }

                lbContent.InnerHtml += "</div>";

                 */

                #endregion

                LiteralVote.Text = mVote.VoteTitle;
                LiteralStat.Text = CreateCharts(mVote);
            }
        }

        protected string CreateCharts(LabMS.Model.Vote vi)
        {
            System.Text.StringBuilder result = new System.Text.StringBuilder();
            System.Text.StringBuilder sbXml = new System.Text.StringBuilder();

            LabMS.BLL.Vote vote = new LabMS.BLL.Vote();
            LabMS.BLL.VoteAnswer voteAnswer = new LabMS.BLL.VoteAnswer();
            LabMS.BLL.VoteItem voteItem = new LabMS.BLL.VoteItem();
            LabMS.BLL.VoteRecordDetail voteRecordDetail = new LabMS.BLL.VoteRecordDetail();

            List<LabMS.Model.VoteItem> voteItems;
            List<LabMS.Model.VoteAnswer> voteAnswers;
            List<LabMS.Model.VoteRecordDetail> voteRecordDetails;

            string[] colors = new string[] {
                "F6BD0F",
                "8BBA00",
                "A66EDD",
                "F984A1",
                "CCCC00",
                "999999",
                "0099CC",
                "FF0000",
                "006F00",
                "0099FF",
                "FF66CC",
                "669966",
                "7C7CB4",
                "FF9933",
                "9900FF",
                "99FFCC",
                "CCCCFF",
                "669900",
                "1941A5",
                "AFD8F8"
            };

            voteItems = voteItem.GetModelList(" VoteID = " + vi.key.ToString());

            Random random = new Random();

            foreach (LabMS.Model.VoteItem vii in voteItems)
            {
                sbXml = new System.Text.StringBuilder();
                sbXml.Append("<graph caption='");
                sbXml.Append(HandleString(vii.VoteItemContent));
                sbXml.Append("' xAxisName='评价项' yAxisName='票数' decimalPrecision='0' formatNumberScale='0'>");

                voteAnswers = voteAnswer.GetModelList(" VoteItemID = " + vii.key.ToString());


                int i = Math.Abs(random.Next());
                i = random.Next(i) / colors.Length;

                foreach (LabMS.Model.VoteAnswer vai in voteAnswers)
                {
                    voteRecordDetails = voteRecordDetail.GetModelList(
                        " VoteItemID = " +
                        vii.key.ToString() +
                        " and VoteAnswerID = " +
                        vai.key.ToString()
                    );

                    sbXml.Append(string.Format("<set name='{0}' value='{1}' color='{2}' />",
                       HandleString(vai.VoteAnswerContent), voteRecordDetails.Count, colors[i++ % colors.Length]));
                }
                sbXml.Append("</graph>");

                result.Append(
                        InfoSoftGlobal.FusionCharts.RenderChartHTML("../FusionCharts/FCF_Bar2D.swf",
                            "",
                            sbXml.ToString(), "item" + vii.key, "600", "200", false)
                    );
            }

            return result.ToString();
        }

        protected string HandleString(string str)
        {
            str = str.Replace("&", "%26amp;");
            str = str.Replace(">", "%26gt;");
            str = str.Replace("'", "%26apos;");

            return str;
        }
    }
}
